//
//  ClubTipsVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/19.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ClubTipsVC.h"
#import "CreateTipsVC.h"
@interface ClubTipsVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ClubTipsVC
{
    UITableView *_tableView;
    
    //请求下来的字典
    NSDictionary *_responseDic;
    //请求下来的数组
    NSArray *_responseArr;
}
- (void)viewWillAppear:(BOOL)animated{
    [self requsetForTipsList];
    [_tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    self.title = @"旅游Tips";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"tianjia"] style:UIBarButtonItemStylePlain target:self action:@selector(createTips)];
    
//    [self requsetForTipsList];
    [self initTableViewWithTips];
    //去除多余的分割线
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)requsetForTipsList{
    //参数
    NSDictionary *parameters = @{
//                                 @"title":_titleTf.text,
//                                 @"content": _tipsTV.text,
//                                 @"clubID": [NSString readToNSDefaultsWithKey:kClubId],
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/club/tipsCurrList.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD showWithStatus:@"请求中"];
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //创建成功
            [SVProgressHUD showSuccessWithStatus:@"请求Tips列表成功"];
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}
- (void)initTableViewWithTips{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
#pragma - mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //
    NSString *_titleStr = _responseDic[@"list"][indexPath.row][@"title"];
    CGSize _titleStrSize = [BAAutoSizeWithWH BA_AutoSizeOfHeghtWithText:_titleStr font:[UIFont systemFontOfSize:17] width:(KScreenWidth - 40)];
    
    UILabel *titilLb = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, KScreenWidth - 40, _titleStrSize.height)];
    titilLb.font = [UIFont systemFontOfSize:17];
    titilLb.text = _titleStr;
    [cell.contentView addSubview:titilLb];
    
    NSString *_contentStr = _responseDic[@"list"][indexPath.row][@"content"];
    CGSize _contentStrSize = [BAAutoSizeWithWH BA_AutoSizeOfHeghtWithText:_contentStr font:[UIFont systemFontOfSize:13] width:(KScreenWidth - 40)];
    UILabel *contentLb = [[UILabel alloc]initWithFrame:CGRectMake(20, _titleStrSize.height + 15, KScreenWidth - 40, _contentStrSize.height)];
    contentLb.textColor = Gray_Color;
    contentLb.font = [UIFont systemFontOfSize:13];
    contentLb.numberOfLines = 0;
    contentLb.text = _contentStr;
    [cell.contentView addSubview:contentLb];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *_titleStr = _responseDic[@"list"][indexPath.row][@"title"];
    CGSize _titleStrSize = [BAAutoSizeWithWH BA_AutoSizeOfHeghtWithText:_titleStr font:[UIFont systemFontOfSize:17] width:(KScreenWidth - 40)];
    NSString *_contentStr = _responseDic[@"list"][indexPath.row][@"content"];
    CGSize _contentStrSize = [BAAutoSizeWithWH BA_AutoSizeOfHeghtWithText:_contentStr font:[UIFont systemFontOfSize:13] width:(KScreenWidth - 40)];
    return 30 + _titleStrSize.height + _contentStrSize.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        //先把需要删除的TipsId上传服务器
        NSString *idStr = _responseDic[@"list"][indexPath.row][@"id"];
        NSDictionary *parameters = @{
                                     
                                     @"id": idStr,
                                     
                                     };
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer=[AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        [manager POST:@"http://115.28.152.130/tour/v1/club/tipsDelete.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
            _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            NSLog(@"%@>>>>",_responseDic);
            _responseArr = _responseDic[@"list"];
            NSString *error = _responseDic[@"code"];
            if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
                [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
            }else{
                //删除请求成功后，再次请求新的数据
                [self requsetForTipsList];
                [SVProgressHUD showSuccessWithStatus:@"删除成功"];
            }
            
        } failure:^(NSURLSessionDataTask *operation, NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"删除tips失败"];
        }];
    }
}
- (void)createTips{
    CreateTipsVC *vc = [[CreateTipsVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
